//
//  ViewController.m
//  TextFont
//
//  Created by JoyWang on 14-7-24.
//  Copyright (c) 2014年 tiewang. All rights reserved.
//

#import "ViewController.h"
#import "PreviewViewController.h"
#import "KZColorPicker.h"
@interface ViewController ()<UITextFieldDelegate>
{
    BOOL changeIsColor;
}
@property (strong, nonatomic) IBOutlet UITextField *textView;
@property (strong, nonatomic) IBOutlet UIView *colorView;
@property (strong, nonatomic) IBOutlet UILabel *sizeView;
@property (strong, nonatomic) IBOutlet UIView *backgroundColorView;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *itemRight;
@property (strong, nonatomic) UIColor *theColor;
@property (strong, nonatomic) NSString *theText;
@property (strong, nonatomic) UIColor *theBackgroundColor;
@property ( nonatomic)NSInteger theFont;
@property (strong, nonatomic)UIView *containerView;
@property (strong, nonatomic)KZColorPicker *colorPicker;
@end

@implementation ViewController

#pragma mark - textFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    
    return YES;
}

#pragma mark - private method
-(void)showColorPicker{
    [UIView animateWithDuration:0.5 animations:^{
        self.containerView.alpha = 1.0;
    }completion:^(BOOL finished) {
        [_itemRight setTitle:@"确定"];
    }];
}

-(void)hideColorPicker{
    [UIView animateWithDuration:0.5 animations:^{
        self.containerView.alpha = 0.0;
    }completion:^(BOOL finished) {
        [_itemRight setTitle:@"预览"];
    }];
}

-(void)refeshColorsView{
    [_colorView setBackgroundColor:self.theColor];
    [_backgroundColorView setBackgroundColor:self.theBackgroundColor];
}
#pragma mark - user action
- (IBAction)changeColor:(id)sender {
    changeIsColor =  YES;
    [self showColorPicker];
}
- (IBAction)changeSize:(id)sender {
    UISlider *slider = (UISlider *)sender;
    CGFloat value = slider.value;
    self.theFont = value *10+10;
    _sizeView.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:self.theFont];
    [_sizeView setText:[NSString stringWithFormat:@"%d",self.theFont]];
}
- (IBAction)changeBackgroundColor:(id)sender {
    changeIsColor = NO;
    [self showColorPicker];
}

- (void) pickerChanged:(KZColorPicker *)cp
{
    if (changeIsColor) {
        self.theColor = cp.selectedColor;
    }else{
        self.theBackgroundColor = cp.selectedColor;
    }
    
}
#pragma mark - property
//KZColorPicker 来源 ： https://github.com/alexrestrepo/KZColorPicker

-(UIView *)containerView{
    if (_containerView) {
        return _containerView;
    }
    CGRect rect = CGRectMake(0, SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")*64,CGRectGetWidth([[UIScreen mainScreen] applicationFrame]), CGRectGetHeight([[UIScreen mainScreen] applicationFrame])-SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")*44);
    _containerView = [[UIView alloc] initWithFrame:rect];
    _containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
	_containerView.backgroundColor = [UIColor clearColor];
    _containerView.alpha = 0.0;
    return _containerView;
}

-(KZColorPicker *)colorPicker{
    if (_colorPicker) {
        return _colorPicker;
    }
    _colorPicker = [[KZColorPicker alloc] initWithFrame:self.containerView.bounds];
    _colorPicker.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
	_colorPicker.selectedColor = self.theColor;
    _colorPicker.oldColor = self.theColor;
	[_colorPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
    return _colorPicker;
}

-(UIColor *)theColor{
    if (_theColor) {
        return _theColor;
    }
    _theColor = [UIColor whiteColor];
    return _theColor;
}
-(UIColor *)theBackgroundColor{
    if (_theBackgroundColor) {
        return _theBackgroundColor;
    }
    _theBackgroundColor = [UIColor blackColor];
    return _theBackgroundColor;
}
-(NSString *)theText{
    if (_theText) {
        return _theText;
    }
    _theText = @"Hello，你好";
    return _theText;
}

-(NSInteger)theFont{
    if (_theFont) {
        return _theFont;
    }
    _theFont = 15;
    return _theFont;
}
#pragma mark - life cycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    [_textView setText:self.theText];
    [_colorView setBackgroundColor:self.theColor];
    [_backgroundColorView setBackgroundColor:self.theBackgroundColor];
    [_sizeView setText:[NSString stringWithFormat:@"%ld",self.theFont]];
    
    [self.view addSubview:self.containerView];
    [self.containerView addSubview:self.colorPicker];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



 #pragma mark - Navigation

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if ([_itemRight.title isEqualToString:@"确定"]) {
        [self refeshColorsView];
        [self hideColorPicker];
        return NO;
    }
    return YES;
}
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
     PreviewViewController *preview = [segue destinationViewController];
     preview.theColor = _theColor;
     preview.theFont = _theFont;
     preview.theText = _textView.text;
     preview.theBackgroundColor = _theBackgroundColor;
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }

@end
